home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / watcom / w_modex / xblitbuf.cpp < prev    next >
C/C++ Source or Header  |  1994-02-05  |  22KB  |  1,028 lines

  1. #include <conio.h>
  2. #include <mem.h>
  3. #include <stdio.h>
  4.  
  5. #include "xtypes.hpp"
  6. #include "modex.hpp"
  7. #include "xpal.hpp"
  8. #include "xblitbuf.hpp"
  9.  
  10. #define SEQU_ADDR       0x3C4
  11. #define GRACON_ADDR     0x3CE
  12.  
  13.  
  14. void
  15. clear_blitbuf(blitbuf *buf)
  16. {
  17.     buf->xsize = 0;
  18.     buf->ysize = 0;
  19.  
  20.     delete buf->image;
  21. }
  22.  
  23.  
  24. void
  25. fill_blitbuf(BYTE color, blitbuf *buf)
  26. {
  27.     memset(buf->image, color, buf->xsize * buf->ysize);
  28. }
  29.  
  30.  
  31. void
  32. alloc_blitbuf(blitbuf *buf, DIST xsize, DIST ysize)
  33. {
  34.     buf->xsize = xsize;
  35.     buf->ysize = ysize;
  36.     buf->image = new BYTE[xsize * ysize];
  37. }
  38.  
  39.  
  40. void
  41. lin_2_pln_blitbuf(blitbuf *buf)
  42. {
  43.     int i, j, size;
  44.     BYTE *oldbuf;
  45.     BYTE *newbuf;
  46.     BYTE *src_ptr;
  47.     BYTE *dest_ptr;
  48.  
  49.     oldbuf = buf->image;
  50.  
  51.     size = (buf->xsize * buf->ysize);
  52.     newbuf = new BYTE[size];
  53.     size = (size >> 2);
  54.  
  55.     dest_ptr = newbuf;
  56.  
  57.     for (i=0; i < 4; i++) {
  58.         src_ptr = oldbuf + i;
  59.  
  60.         j=size;
  61.         while (j--) {
  62.             *dest_ptr++ = *src_ptr;
  63.             src_ptr += 4;
  64.         }
  65.     }
  66.  
  67.     buf->image = newbuf;
  68.     delete oldbuf;
  69. }
  70.  
  71.  
  72. void
  73. pln_2_lin_blitbuf(blitbuf *buf)
  74. {
  75.     int i, j, size;
  76.     BYTE *oldbuf;
  77.     BYTE *newbuf;
  78.     BYTE *src_ptr;
  79.     BYTE *dest_ptr;
  80.  
  81.     oldbuf = buf->image;
  82.  
  83.     size = (buf->xsize * buf->ysize);
  84.     newbuf = new BYTE[size];
  85.     size = (size >> 2);
  86.  
  87.     src_ptr = oldbuf;
  88.     for (i=0; i < 4; i++) {
  89.         dest_ptr = newbuf + i;
  90.  
  91.         j=size;
  92.         while (j--) {
  93.             *dest_ptr = *src_ptr++;
  94.             dest_ptr += 4;
  95.         }
  96.     }
  97.  
  98.     buf->image = newbuf;
  99.     delete oldbuf;
  100. }
  101.  
  102.  
  103. void
  104. vanilla_bitblitX(COORD x, COORD y, blitbuf *buf)
  105. {
  106.     short int ysize, plane, i, j, loop, skip, rewind, len;
  107.     short int xsize[4];
  108.     BYTE *base_vga;
  109.     BYTE *vga_ptr;
  110.     BYTE *buf_ptr;
  111.  
  112.     // Length of bitmap in each plane
  113.     plane = (x & 3);
  114.     i = buf->xsize + plane - 1;
  115.     xsize[0] = ((i--) >> 2);
  116.     xsize[1] = ((i--) >> 2);
  117.     xsize[2] = ((i--) >> 2);
  118.     xsize[3] = (i >> 2) + 1;
  119.  
  120.     for (i=plane; i < 3; i++) {
  121.         xsize[i]++;
  122.     }
  123.  
  124.     ysize = buf->ysize;
  125.     base_vga = RowsX[y] + (x >> 2) + activeStart;
  126.     write_plane = -1;
  127.  
  128.     for (loop = 0; loop < 4; loop++) {
  129.         len = xsize[plane];
  130.         rewind = buf->xsize - (len << 2);
  131.         skip = widthBytes - len;
  132.         buf_ptr = buf->image + loop;
  133.         vga_ptr = base_vga;
  134.  
  135.         outpw(SEQU_ADDR, plane_mask[plane++]);
  136.         if (plane == 4) {
  137.             plane = 0;
  138.             base_vga++;
  139.         }
  140.  
  141.         i=ysize;
  142.         while (i--) {
  143.             j=len;
  144.             while (j--) {
  145.                 *vga_ptr++ = *buf_ptr;
  146.                 buf_ptr += 4;
  147.             }
  148.  
  149.             buf_ptr += rewind;
  150.             vga_ptr += skip;
  151.         }
  152.     }
  153. }
  154.  
  155.  
  156. void
  157. vanilla_getblitX(COORD x, COORD y, blitbuf *buf)
  158. {
  159.     // Do nothing
  160. }
  161.  
  162.  
  163. void
  164. aligned_bitblitX(COORD x, COORD y, blitbuf *buf)
  165. {
  166.     short int i, j, plane, skip, xsize, ysize;
  167.     BYTE *base_vga;
  168.     BYTE *vga_ptr;
  169.     BYTE *buf_ptr;
  170.  
  171.     xsize = (buf->xsize >> 2);
  172.     ysize = buf->ysize;
  173.     skip = widthBytes - xsize;
  174.     base_vga = RowsX[y] + (x >> 2) + activeStart;
  175.  
  176.     for (plane=0; plane < 4; plane++) {
  177.         buf_ptr = buf->image + plane;
  178.         vga_ptr = base_vga;
  179.  
  180.         outpw(SEQU_ADDR, plane_mask[plane]);
  181.  
  182.         i=ysize;
  183.         while (i--) {
  184.             j=xsize;
  185.             while (j--) {
  186.                 *vga_ptr++ = *buf_ptr;
  187.                 buf_ptr += 4;
  188.             }
  189.             vga_ptr += skip;
  190.         }
  191.     }
  192.  
  193.     write_plane = 3;
  194. }
  195.  
  196.  
  197. void
  198. aligned_getblitX(COORD x, COORD y, blitbuf *buf)
  199. {
  200.     short int i, j, plane, skip, xsize, ysize;
  201.     BYTE *base_vga;
  202.     BYTE *vga_ptr;
  203.     BYTE *buf_ptr;
  204.  
  205.     xsize = (buf->xsize >> 2);
  206.     ysize = buf->ysize;
  207.     skip = widthBytes - xsize;
  208.     base_vga = RowsX[y] + (x >> 2) + activeStart;
  209.  
  210.     for (plane=0; plane < 4; plane++) {
  211.         buf_ptr = buf->image + plane;
  212.         vga_ptr = base_vga;
  213.  
  214.         outpw(GRACON_ADDR, read_mask[plane]);
  215.  
  216.         i=ysize;
  217.         while (i--) {
  218.             j=xsize;
  219.             while (j--) {
  220.                 *buf_ptr = *vga_ptr++;
  221.                 buf_ptr += 4;
  222.             }
  223.             vga_ptr += skip;
  224.         }
  225.     }
  226.  
  227.     read_plane = 3;
  228. }
  229.  
  230.  
  231. void
  232. transparent_bitblitX(COORD x, COORD y, blitbuf *buf)
  233. {
  234.     short int i, j, plane, skip, xsize, ysize;
  235.     BYTE *base_vga;
  236.     BYTE *vga_ptr;
  237.     BYTE *buf_ptr;
  238.  
  239.     xsize = (buf->xsize >> 2);
  240.     ysize = buf->ysize;
  241.     skip = widthBytes - xsize;
  242.     base_vga = RowsX[y] + (x >> 2) + activeStart;
  243.  
  244.     for (plane=0; plane < 4; plane++) {
  245.         buf_ptr = buf->image + plane;
  246.         vga_ptr = base_vga;
  247.  
  248.         outpw(SEQU_ADDR, plane_mask[plane]);
  249.  
  250.         i=ysize;
  251.         while (i--) {
  252.             j=xsize;
  253.             while (j--) {
  254.                 if (*buf_ptr) {
  255.                     *vga_ptr = *buf_ptr;
  256.                 }
  257.                 vga_ptr++;
  258.                 buf_ptr += 4;
  259.             }
  260.             vga_ptr += skip;
  261.         }
  262.     }
  263.  
  264.     write_plane = 3;
  265. }
  266.  
  267.  
  268. void
  269. planar_bitblitX(COORD x, COORD y, blitbuf *buf)
  270. {
  271.     short int i, plane, xsize, ysize;
  272.     BYTE *base_vga;
  273.     BYTE *vga_ptr;
  274.     BYTE *buf_ptr;
  275.  
  276.     xsize = (buf->xsize >> 2);
  277.     ysize = buf->ysize;
  278.     base_vga = RowsX[y] + (x >> 2) + activeStart;
  279.     buf_ptr = buf->image;
  280.  
  281.     for (plane=0; plane < 4; plane++) {
  282.         vga_ptr = base_vga;
  283.  
  284.         outpw(SEQU_ADDR, plane_mask[plane]);
  285.  
  286.         i=ysize;
  287.         while (i--) {
  288.             memcpy(vga_ptr, buf_ptr, xsize);
  289.             vga_ptr += widthBytes;
  290.             buf_ptr += xsize;
  291.         }
  292.     }
  293.  
  294.     write_plane = 3;
  295. }
  296.  
  297.  
  298. void
  299. planar_getblitX(COORD x, COORD y, blitbuf *buf)
  300. {
  301.     short int i, plane, xsize, ysize;
  302.     BYTE *base_vga;
  303.     BYTE *vga_ptr;
  304.     BYTE *buf_ptr;
  305.  
  306.     xsize = (buf->xsize >> 2);
  307.     ysize = buf->ysize;
  308.     base_vga = RowsX[y] + (x >> 2) + activeStart;
  309.     buf_ptr = buf->image;
  310.  
  311.     for (plane=0; plane < 4; plane++) {
  312.         vga_ptr = base_vga;
  313.  
  314.         outpw(GRACON_ADDR, read_mask[plane]);
  315.  
  316.         i=ysize;
  317.         while (i--) {
  318.             memcpy(buf_ptr, vga_ptr, xsize);
  319.             vga_ptr += widthBytes;
  320.             buf_ptr += xsize;
  321.         }
  322.     }
  323.  
  324.     read_plane = 3;
  325. }
  326.  
  327.  
  328. void
  329. wide_bitblitX(COORD y, blitbuf *buf)
  330. {
  331.     short int bufsize;
  332.     BYTE *vga_ptr;
  333.     BYTE *buf_ptr;
  334.  
  335.     write_plane = 3;
  336.     buf_ptr = buf->image;
  337.     vga_ptr = RowsX[y] + activeStart;
  338.     bufsize = (buf->ysize * widthBytes);
  339.  
  340.     outpw(SEQU_ADDR, PLANE_0);
  341.     memcpy(vga_ptr, buf_ptr, bufsize);
  342.     buf_ptr += bufsize;
  343.  
  344.     outpw(SEQU_ADDR, PLANE_1);
  345.     memcpy(vga_ptr, buf_ptr, bufsize);
  346.     buf_ptr += bufsize;
  347.  
  348.     outpw(SEQU_ADDR, PLANE_2);
  349.     memcpy(vga_ptr, buf_ptr, bufsize);
  350.     buf_ptr += bufsize;
  351.  
  352.     outpw(SEQU_ADDR, PLANE_3);
  353.     memcpy(vga_ptr, buf_ptr, bufsize);
  354. }
  355.  
  356.  
  357. void
  358. wide_getblitX(COORD y, blitbuf *buf)
  359. {
  360.     short int bufsize;
  361.     BYTE *vga_ptr;
  362.     BYTE *buf_ptr;
  363.  
  364.     read_plane = 3;
  365.     buf_ptr = buf->image;
  366.     vga_ptr = RowsX[y] + activeStart;
  367.     bufsize = (buf->ysize * widthBytes);
  368.  
  369.     outpw(GRACON_ADDR, READ_PLANE_0);
  370.     memcpy(buf_ptr, vga_ptr, bufsize);
  371.     buf_ptr += bufsize;
  372.  
  373.     outpw(GRACON_ADDR, READ_PLANE_1);
  374.     memcpy(buf_ptr, vga_ptr, bufsize);
  375.     buf_ptr += bufsize;
  376.  
  377.     outpw(GRACON_ADDR, READ_PLANE_2);
  378.     memcpy(buf_ptr, vga_ptr, bufsize);
  379.     buf_ptr += bufsize;
  380.  
  381.     outpw(GRACON_ADDR, READ_PLANE_3);
  382.     memcpy(buf_ptr, vga_ptr, bufsize);
  383. }
  384.  
  385.  
  386. void
  387. save_blitbufPCX(char *fname, blitbuf *buf)
  388. {
  389.     FILE *fp;
  390.     unsigned int i, size, temp_int;
  391.     BYTE VGA_pal[768];
  392.     BYTE *buf_ptr;
  393.     BYTE temp_char, match, count;
  394.  
  395.     fp = fopen(fname, "wb");
  396.  
  397.     if (fp != NULL) {
  398.         // Write manufacturer's byte
  399.         temp_char = 10;
  400.         fwrite(&temp_char, 1, 1, fp);
  401.  
  402.         // Write version of PCX.  5 = 256 color (PCX Version 5.0)
  403.         temp_char = 5;
  404.         fwrite(&temp_char, 1, 1, fp);
  405.  
  406.         // Write encoding type, always 1 for RLE.
  407.         temp_char = 1;
  408.         fwrite(&temp_char, 1, 1, fp);
  409.  
  410.         // Write bits_per_pixel = 8.
  411.         temp_char = 8;
  412.         fwrite(&temp_char, 1, 1, fp);
  413.  
  414.         // Write starting X and Y coords
  415.         temp_int = 0;
  416.         fwrite(&temp_int, 2, 1, fp);
  417.         fwrite(&temp_int, 2, 1, fp);
  418.  
  419.         // Write X size
  420.         temp_int = (buf->xsize - 1);
  421.         fwrite(&temp_int, 2, 1, fp);
  422.  
  423.         // Write Y size
  424.         temp_int = (buf->ysize - 1);
  425.         fwrite(&temp_int, 2, 1, fp);
  426.  
  427.         // Do HRES and VRES **
  428.         temp_int = buf->xsize;
  429.         fwrite(&temp_int, 2, 1, fp);
  430.         temp_int = buf->ysize;
  431.         fwrite(&temp_int, 2, 1, fp);
  432.  
  433.         // Write 16 color palette, not used.
  434.         temp_int = 0;
  435.         i=24;
  436.         while (i--) {
  437.             fwrite(&temp_int, 2, 1, fp);
  438.         }
  439.  
  440.         // Write vmode byte.
  441.         temp_char = 0;
  442.         fwrite(&temp_char, 1, 1, fp);
  443.  
  444.         // Write bit_planes
  445.         temp_char = 1;
  446.         fwrite(&temp_char, 1, 1, fp);
  447.  
  448.         // Write bytes_per_line
  449.         temp_int = buf->xsize;
  450.         fwrite(&temp_int, 2, 1, fp);
  451.  
  452.         // Write palette type
  453.         temp_int = 1;
  454.         fwrite(&temp_int, 2, 1, fp);
  455.  
  456.         // Write junk filler
  457.         temp_int = 0;
  458.         i=29;
  459.         while (i--) {
  460.             fwrite(&temp_int, 2, 1, fp);
  461.         }
  462.  
  463.         // Write the actual image
  464.         buf_ptr = buf->image;
  465.         size = (buf->xsize * buf->ysize);
  466.  
  467.         count = 0;
  468.         match = *buf_ptr;
  469.  
  470.         i=size;
  471.         while (i--) {
  472.             temp_char = *buf_ptr++;
  473.  
  474.             if ((temp_char == match) && (count < 63)) {
  475.                count++;
  476.             } else {
  477.                 if ((count == 1) && (match < 192)) {
  478.                     // Write single byte
  479.                     fwrite(&match,1,1,fp);
  480.                 } else {
  481.                     // Write run of pixels
  482.                     count += 192;
  483.                     fwrite(&count, 1, 1, fp);
  484.                     fwrite(&match, 1, 1, fp);
  485.                 }
  486.                 count = 1;
  487.                 match = temp_char;
  488.             }
  489.         }
  490.  
  491.         if ((count == 1) && (match < 192)) {
  492.             // Write single byte
  493.             fwrite(&match,1,1,fp);
  494.         } else {
  495.             // Write run of pixels
  496.             count += 192;
  497.             fwrite(&count, 1, 1, fp);
  498.             fwrite(&match, 1, 1, fp);
  499.         }
  500.  
  501.         // Write palette verification byte
  502.         temp_char = 12;
  503.         fwrite(&temp_char, 1, 1, fp);
  504.  
  505.         get_paletteX(VGA_pal);
  506.  
  507.         // Write 256 color palette
  508.         fwrite(VGA_pal, 1, 768, fp);
  509.  
  510.         fclose(fp);
  511.     }
  512. }
  513.  
  514.  
  515. int
  516. load_blitbufPCX(char *fname, blitbuf *buf)
  517. {
  518.     FILE *fp;
  519.     int size;
  520.     BYTE VGA_pal[768];
  521.     BYTE PCX_byte, RLE_byte;
  522.     BYTE *buf_ptr;
  523.     BYTE *end_of_buf;
  524.  
  525.     fp = fopen(fname, "rb");
  526.  
  527.     if (fp == NULL) {
  528.         buf->xsize = 0;
  529.         buf->ysize = 0;
  530.         buf->image = NULL;
  531.         return 0;
  532.     } else {
  533.         fseek(fp, 8, SEEK_SET);
  534.         fread(&buf->xsize, 2, 1, fp);
  535.         fread(&buf->ysize, 2, 1, fp);
  536.  
  537.         buf->xsize++;
  538.         buf->ysize++;
  539.  
  540.         size = (buf->xsize * buf->ysize);
  541.  
  542.         buf->image = new BYTE[size];
  543.         buf_ptr = buf->image;
  544.         end_of_buf = buf_ptr + size;
  545.  
  546.         // Load 256 color PCX palette
  547.         fseek(fp, -768, SEEK_END);
  548.         fread(VGA_pal, 1, 768, fp);
  549.  
  550.         set_paletteX(VGA_pal);
  551.  
  552.         fseek(fp, 128, SEEK_SET);
  553.  
  554.         while (buf_ptr < end_of_buf) {
  555.             // Read next packet
  556.             fread(&PCX_byte, 1, 1, fp);
  557.  
  558.             if (PCX_byte < 192) {
  559.                 // Raw Pixel
  560.                 *buf_ptr++ = PCX_byte;
  561.             } else {
  562.                 // RLE Pixels
  563.                 PCX_byte = PCX_byte & 0x3F;
  564.                 fread(&RLE_byte, 1, 1, fp);
  565.                 memset(buf_ptr, RLE_byte, PCX_byte);
  566.                 buf_ptr += PCX_byte;
  567.             }
  568.         }
  569.  
  570.         fclose(fp);
  571.         return 1;
  572.     }
  573. }
  574.  
  575.  
  576. void
  577. scale_blitbuf(DIST dest_x, DIST dest_y, blitbuf *buf1, blitbuf *buf2)
  578. {
  579.     unsigned long ErrorAccX, ErrorAccY, ErrorAdjX, ErrorAdjY;
  580.     DIST oldx, oldy, newx, newy;
  581.     short int i, j, count;
  582.     BYTE *src_base;
  583.     BYTE *src_ptr;
  584.     BYTE *dest_ptr;
  585.     BYTE *newbuf;
  586.  
  587.     oldx = buf1->xsize;
  588.     oldy = buf1->ysize;
  589.     newx = dest_x;
  590.     newy = dest_y;
  591.  
  592.     newbuf = new BYTE[newx * newy];
  593.  
  594.     src_base = buf1->image;
  595.     dest_ptr = newbuf;
  596.  
  597.     // My bitmap scaling routine.  As you probably noticed, it's
  598.     // pretty Bresenhammy!
  599.  
  600.     ErrorAccY = 0x8000;
  601.  
  602.     if (newx > oldx) {
  603.         // Biggering
  604.         ErrorAdjX = ((((unsigned long)newx) << 16) /
  605.                      (((unsigned long)oldx)));
  606.  
  607.         ErrorAdjY = ((((unsigned long)newy) << 16) /
  608.                      (((unsigned long)oldy)));
  609.  
  610.         i=oldy;
  611.         while (i--) {
  612.             ErrorAccX = 0x8000;
  613.             src_ptr = src_base;
  614.  
  615.             j=oldx;
  616.             while (j--) {
  617.                 ErrorAccX += ErrorAdjX;
  618.                 if (count = (ErrorAccX >> 16)) {
  619.                     ErrorAccX &= 0xFFFFL;
  620.                     while (count--) {
  621.                         *dest_ptr++ = *src_ptr;
  622.                     }
  623.                 }
  624.                 src_ptr++;
  625.             }
  626.  
  627.             ErrorAccY += ErrorAdjY;
  628.             count = (ErrorAccY >> 16) - 1;
  629.             while (count--) {
  630.                 memcpy(dest_ptr, dest_ptr - newx, newx);
  631.                 dest_ptr += newx;
  632.             }
  633.             ErrorAccY &= 0xFFFFL;
  634.             src_base += oldx;
  635.         }
  636.     } else {
  637.         // Smallering
  638.         ErrorAdjX = ((((unsigned long)oldx) << 16) /
  639.                      (((unsigned long)newx)));
  640.  
  641.         ErrorAdjY = ((((unsigned long)oldy) << 16) /
  642.                      (((unsigned long)newy)));
  643.  
  644.         i=newy;
  645.         while (i--) {
  646.             ErrorAccX = 0x8000;
  647.             src_ptr = src_base;
  648.  
  649.             j=newx;
  650.             while (j--) {
  651.                 *dest_ptr++ = *src_ptr;
  652.                 ErrorAccX += ErrorAdjX;
  653.                 src_ptr += (ErrorAccX >> 16);
  654.                 ErrorAccX &= 0xFFFFL;
  655.             }
  656.  
  657.             ErrorAccY += ErrorAdjY;
  658.             src_base += (oldx * (ErrorAccY >> 16));
  659.             ErrorAccY &= 0xFFFFL;
  660.         }
  661.     }
  662.  
  663.     if (buf2 == NULL) {
  664.         delete buf1->image;
  665.         buf1->xsize = newx;
  666.         buf1->ysize = newy;
  667.         buf1->image = newbuf;
  668.     } else {
  669.         if (buf2->image != NULL) {
  670.             delete buf2->image;
  671.         }
  672.         buf2->xsize = newx;
  673.         buf2->ysize = newy;
  674.         buf2->image = newbuf;
  675.     }
  676. }
  677.  
  678.  
  679. void
  680. vertical_scale_blitbuf(DIST dest_y, blitbuf *buf1, blitbuf *buf2)
  681. {
  682.     unsigned long ErrorAccY, ErrorAdjY;
  683.     DIST xsize, oldy, newy;
  684.     short int i, count;
  685.     BYTE *src_ptr;
  686.     BYTE *dest_ptr;
  687.     BYTE *newbuf;
  688.  
  689.     xsize = buf1->xsize;
  690.     oldy = buf1->ysize;
  691.     newy = dest_y;
  692.  
  693.     newbuf = new BYTE[xsize * newy];
  694.  
  695.     src_ptr = buf1->image;
  696.     dest_ptr = newbuf;
  697.  
  698.     // My bitmap scaling routine.  As you probably noticed, it's
  699.     // pretty Bresenhammy!
  700.  
  701.     ErrorAccY = 0x8000;
  702.     ErrorAdjY = ((((unsigned long)newy) << 16) /
  703.                  (((unsigned long)oldy)));
  704.  
  705.     if (newy >= oldy) {
  706.         // Biggering
  707.         i=oldy;
  708.         while (i--) {
  709.             ErrorAccY += ErrorAdjY;
  710.             if (count = (ErrorAccY >> 16)) {
  711.                 ErrorAccY &= 0xFFFFL;
  712.  
  713.                 while (count--) {
  714.                     memcpy(dest_ptr, src_ptr, xsize);
  715.                     dest_ptr += xsize;
  716.                 }
  717.             }
  718.  
  719.             src_ptr += xsize;
  720.         }
  721.     } else {
  722.         // Smallering
  723.         i=oldy;
  724.         while (i--) {
  725.             ErrorAccY += ErrorAdjY;
  726.             if (ErrorAccY & ~0xFFFFL) {
  727.                 ErrorAccY &= 0xFFFFL;
  728.                 memcpy(dest_ptr, src_ptr, xsize);
  729.                 dest_ptr += xsize;
  730.             }
  731.  
  732.             src_ptr += xsize;
  733.         }
  734.     }
  735.  
  736.     if (buf2 == NULL) {
  737.         delete buf1->image;
  738.         buf1->ysize = newy;
  739.         buf1->image = newbuf;
  740.     } else {
  741.         if (buf2->image != NULL) {
  742.             delete buf2->image;
  743.         }
  744.         buf2->xsize = xsize;
  745.         buf2->ysize = newy;
  746.         buf2->image = newbuf;
  747.     }
  748. }
  749.  
  750.  
  751. void
  752. greyscale_blitbuf(blitbuf *buf)
  753. {
  754.     BYTE temp_pal[768];
  755.     BYTE *buf_ptr;
  756.     BYTE *temp;
  757.     BYTE r, g;
  758.     unsigned int i;
  759.  
  760.     buf_ptr = buf->image;
  761.  
  762.     get_paletteX(temp_pal, 0);
  763.  
  764.     for (i = (buf->xsize * buf->ysize); i; i--) {
  765.         temp = temp_pal + ((*buf_ptr) * 3);
  766.         r = *temp++;
  767.         g = *temp++;
  768.  
  769.         *buf_ptr++ = ((r * 19) + (g * 37) + (*temp << 3)) >> 6;
  770.     }
  771. }
  772.  
  773.  
  774. void
  775. RGB_blitbuf(blitbuf *buf)
  776. {
  777.     BYTE temp_pal[768];
  778.     BYTE *buf_ptr;
  779.     BYTE *temp;
  780.     BYTE r, g, b;
  781.     unsigned int i;
  782.  
  783.     buf_ptr = buf->image;
  784.  
  785.     get_paletteX(temp_pal, 0);
  786.  
  787.     for (i = (buf->xsize * buf->ysize); i; i--) {
  788.         temp = temp_pal + ((*buf_ptr) * 3);
  789.         r = (*temp) + 4;
  790.         temp++;
  791.         g = (*temp) + 4;
  792.         temp++;
  793.         b = (*temp) + 8;
  794.  
  795.         *buf_ptr++ = ((r >> 3) << 5) + ((g >> 3) << 2) + (b >> 4);
  796.     }
  797. }
  798.  
  799.  
  800. void
  801. flip_vertical_blitbuf(blitbuf *buf)
  802. {
  803.     BYTE *top;
  804.     BYTE *bottom;
  805.     BYTE *temp;
  806.     DIST i, x, y;;
  807.  
  808.     x = buf->xsize;
  809.     y = buf->ysize;
  810.  
  811.     temp = new BYTE[x];
  812.  
  813.     top = buf->image;
  814.     bottom = buf->image + (x * (y-1));
  815.  
  816.     i = (y >> 1);
  817.     while (i--) {
  818.         memcpy(temp, top, x);
  819.         memcpy(top, bottom, x);
  820.         memcpy(bottom, temp, x);
  821.         top += x;
  822.         bottom -= x;
  823.     }
  824.  
  825.     delete temp;
  826. }
  827.  
  828.  
  829. void
  830. flip_horizontal_blitbuf(blitbuf *buf)
  831. {
  832.     BYTE *buf_ptr;
  833.     BYTE *temp_ptr;
  834.     BYTE *temp;
  835.     DIST i, j, x;
  836.  
  837.     x = buf->xsize;
  838.  
  839.     temp = new BYTE[x];
  840.  
  841.     buf_ptr = buf->image;
  842.  
  843.     i = buf->ysize;
  844.     while (i--) {
  845.         memcpy(temp, buf_ptr, x);
  846.         temp_ptr = temp + (x - 1);
  847.         j=x;
  848.         while (j--) {
  849.             *buf_ptr++ = *temp_ptr--;
  850.         }
  851.     }
  852.  
  853.     delete temp;
  854. }
  855.  
  856.  
  857. void
  858. brighten_blitbuf(SBYTE factor, blitbuf *buf)
  859. {
  860.     BYTE *buf_ptr;
  861.     short int scratch;
  862.     unsigned int i;
  863.  
  864.     buf_ptr = buf->image;
  865.  
  866.     for (i = (buf->xsize * buf->ysize); i; i--) {
  867.         scratch = (*buf_ptr + factor);
  868.         if (scratch <= 0) {
  869.             *buf_ptr++ = 0;
  870.         } else if (scratch >= 63) {
  871.             *buf_ptr++ = 63;
  872.         } else {
  873.             *buf_ptr++ = scratch;
  874.         }
  875.     }
  876. }
  877.  
  878.  
  879. void
  880. stretch_blitbuf(BYTE factor, blitbuf *buf)
  881. {
  882.     BYTE *buf_ptr;
  883.     short int scratch;
  884.     unsigned int i;
  885.  
  886.     buf_ptr = buf->image;
  887.  
  888.     for (i = (buf->xsize * buf->ysize); i; i--) {
  889.         scratch = ((((*buf_ptr - 32) * factor) + 8) >> 4) + 32;
  890.         if (scratch <= 0) {
  891.             *buf_ptr++ = 0;
  892.         } else if (scratch >= 63) {
  893.             *buf_ptr++ = 63;
  894.         } else {
  895.             *buf_ptr++ = scratch;
  896.         }
  897.     }
  898. }
  899.  
  900.  
  901. void
  902. pixelize(DIST pixfactor, blitbuf *buf)
  903. {
  904.     // Do nothing
  905. }
  906.  
  907.  
  908. void
  909. GREYconvolve_blitbuf(BYTE *kernel, blitbuf *buf)
  910. {
  911.     // Do nothing
  912. }
  913.  
  914.  
  915. void
  916. RGBconvolve_blitbuf(BYTE *kernel, blitbuf *buf)
  917. {
  918.     // Do nothing
  919. }
  920.  
  921.  
  922. void
  923. scale_scanline(BYTE *source, BYTE *dest, DIST smap_size, DIST dmap_size,
  924.     DIST dline_size)
  925. {
  926.     unsigned long ErrorAcc, ErrorAdj;
  927.     short int i, temp, invert;
  928.  
  929.     ErrorAcc = 0x8000;
  930.  
  931.     // Prepare for backwards scanlines
  932.     if (dline_size >= 0) {
  933.         invert = 0;
  934.     } else {
  935.         invert = 1;
  936.         dline_size = -dline_size;
  937.     }
  938.  
  939.     if (dline_size > smap_size) {
  940.         // Biggering
  941.         if (smap_size == 0) {
  942.             return;
  943.         }
  944.         ErrorAdj = ((((unsigned long)dline_size) << 16) /
  945.                     (((unsigned long)smap_size)));
  946.  
  947.         i=smap_size;
  948.         while (i--) {
  949.             ErrorAcc += ErrorAdj;
  950.             temp = (ErrorAcc >> 16);
  951.             ErrorAcc &= 0xFFFFL;
  952.             while (temp--) {
  953.                 *dest++ = *source;
  954.             }
  955.             source++;
  956.         }
  957.     } else {
  958.         // Smallering
  959.         if (dline_size == 0) {
  960.             memset(dest, 0, dmap_size);
  961.         } else {
  962.             temp = dmap_size - dline_size;
  963.             i = temp >> 1;
  964.             temp -= i;
  965.             while (i--) {
  966.                 *dest++ = 0;
  967.             }
  968.  
  969.             ErrorAdj = ((((unsigned long)smap_size) << 16) /
  970.                         (((unsigned long)dline_size)));
  971.  
  972.             i=dline_size;
  973.  
  974.             while (i--) {
  975.                 *dest++ = *source;
  976.                 ErrorAcc += ErrorAdj;
  977.                 source += (ErrorAcc >> 16);
  978.                 ErrorAcc &= 0xFFFFL;
  979.             }
  980.  
  981.             while (temp--) {
  982.                 *dest++ = 0;
  983.             }
  984.         }
  985.     }
  986. }
  987.  
  988.  
  989. int
  990. load_blitbufRAW(char *rawname, char *palname, blitbuf *buf)
  991. {
  992.     FILE *fp;
  993.     BYTE VGA_pal[768];
  994.  
  995.     fp = fopen(rawname, "rb");
  996.  
  997.     if (fp == NULL) {
  998.         buf->xsize = 0;
  999.         buf->ysize = 0;
  1000.         buf->image = NULL;
  1001.         return 0;
  1002.     } else {
  1003.         buf->xsize = 320;
  1004.         buf->ysize = 200;
  1005.         buf->image = new BYTE[64000L];
  1006.  
  1007.         // Load image
  1008.         fread(buf->image, 64000L, 1, fp);
  1009.  
  1010.         if (palname == NULL) {
  1011.             fread(VGA_pal, 1, 768, fp);
  1012.             set_paletteX(VGA_pal);
  1013.             fclose(fp);
  1014.         } else {
  1015.             fclose(fp);
  1016.             fp = fopen(palname, "rb");
  1017.             if (fp != NULL) {
  1018.                 fread(VGA_pal, 1, 768, fp);
  1019.                 set_paletteX(VGA_pal);
  1020.                 fclose(fp);
  1021.             }
  1022.         }
  1023.  
  1024.         return 1;
  1025.     }
  1026. }
  1027.  
  1028.